package org.windblog.core.servlet.category;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.windblog.core.CategoryService;
import org.windblog.core.entity.Category;


@SuppressWarnings("serial")
public class SetCategoryServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String name = request.getParameter("name").trim();
		String slug = request.getParameter("slug");
		String oname = request.getParameter("oname");
		String description = request.getParameter("description");
		
		if (name == null || !name.equals(oname) && CategoryService.isExist(name)) {
			response.sendRedirect("/admin/categories.jsp?message=5");
		} else {
			Category cat = CategoryService.getCategory(oname);
			cat.setName(name);
			cat.setSlug(slug);
			cat.setDescription(description);

			CategoryService.setCategory(cat);
			response.sendRedirect("/admin/categories.jsp?message=2");
		}
	}

}
